package com.sailors.common.task.provider.infra.design.common.util;

import lombok.Getter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
@Getter
public class BeanHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BeanHelper.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过名称获取bean
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return BeanHelper.applicationContext.getBean(name);
    }


    /**
     * 通过Class名称获取bean
     *
     * @param tClass
     * @return
     */
    public static <T> T getBean(Class<T> tClass) {
        return BeanHelper.applicationContext.getBean(tClass);
    }
}
